import 'package:flutter/material.dart';

class DynamicListPage extends StatefulWidget {

  const DynamicListPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _DynamicListPageState();
  }
}

class _DynamicListPageState extends State<DynamicListPage> {
  final names = ["hello","hello"];
  int count = 0;
  ScrollController _scrollController = ScrollController();
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("动态列表"),
      ),
      body: ListView.builder(
          controller: _scrollController,
          itemCount: names.length,
          itemBuilder: (BuildContext context,int index) {
            return Container(
                width: double.infinity,
                height: 50,
                alignment: Alignment.center,
                child: Text(names[index]));
          }),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            count ++;
            names.add("added$count");
            _scrollController.animateTo(
              _scrollController.position.maxScrollExtent,
              duration: Duration(milliseconds: 300),
              curve: Curves.easeInOut,
            );
          });
        },
        child: const Icon(Icons.add),
      ),
    );
  }

}